﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace GameStateManagement
{
    class PowerupManager : Microsoft.Xna.Framework.GameComponent
    {
        public List<PowerUp> powerupList = new List<PowerUp>();
        private Random random;
        private Vector3 tempVector;
        public static float MinRadiusMagnitude = 0.4f;
        public static float MaxRadiusMagnitude = 0.9f;
        public static float PowerUpMaxSpeed = 0.3f;
        Utils.Timer timer = new Utils.Timer();
        int outerbound;
        int outerboundNeg;

        public PowerupManager(Game game)
            : base(game)
        {
            tempVector = new Vector3();
            random = new Random();
        }
        public override void Update(GameTime gameTime)
        {
            timer.Update(gameTime);
            base.Update(gameTime);
        }
        public void CreateSpeedBoost(float speed)
        {
            // Clamp the speed.
            if (speed > CheckpointManager.CheckpointMaxSpeed) speed = CheckpointManager.CheckpointMaxSpeed;

            // Generate float between MinRadiusMagnitude and MaxRadiusMagnitude.
            float magnitudeRatio = MinRadiusMagnitude + ((float)random.NextDouble() * (MaxRadiusMagnitude - MinRadiusMagnitude));
            SpeedBoost boost = new SpeedBoost(Game, magnitudeRatio, speed);
            RandomLocation();
            boost.m_vWorldPosition = tempVector;
            powerupList.Add(boost);
            Game.Components.Add(boost);
        }
        public void CreateShield(float speed)
        {
            // Clamp the speed.
            if (speed > PowerUpMaxSpeed)
                speed = PowerUpMaxSpeed;

            // Generate float between MinRadiusMagnitude and MaxRadiusMagnitude.
            float magnitudeRatio = MinRadiusMagnitude + ((float)random.NextDouble() * (MaxRadiusMagnitude - MinRadiusMagnitude));
            Shield shield = new Shield(Game, magnitudeRatio, speed);
            RandomLocation();
            shield.m_vWorldPosition = tempVector;
            powerupList.Add(shield);
            Game.Components.Add(shield);
        }
        public void setLocation(ShrinkingStarSphere passedSphere)
        {
            CheckpointManager checkpointManager = CheckpointManager.GetCheckpointManager();
            outerbound = (int)passedSphere.GetRadius();
            outerbound -= 100;
            outerboundNeg = outerbound * -1;
            foreach (PowerUp powerup in powerupList)
            {
                if (!powerup.GetDisplayed())
                {
                    RandomLocation();
                    powerup.m_vWorldPosition = tempVector;
                    powerup.SetDisplayed(true);
                }
            }
        }
        public void RandomLocation()
        {
            if (random.Next(2) == 0)
            {
                tempVector.X = random.Next(0, outerbound);
            }
            else
            {
                tempVector.X = random.Next(outerboundNeg, 0);
            }
            if (random.Next(2) == 0)
            {
                tempVector.Y = random.Next(0, outerbound);
            }
            else
            {
                tempVector.Y = random.Next(outerboundNeg, 0);
            }
            if (random.Next(2) == 0)
            {
                tempVector.Z = random.Next(0, outerbound);
            }
            else
            {
                tempVector.Z = random.Next(outerboundNeg, 0);
            }
        }
    }
}